<%inherit file="base.html"/>
<%def name="head()">
<script>
    function busy(flag) {
        if (flag) {
            $("#loading").show();
            $("#inbox").html("")
            $("#inbox").hide();
        } else {
            $("#loading").hide();
            $("#inbox").show();
        }
    }


    function update_status(serv)
    {
        busy(true);
        $.get("/service/"+serv+"/status", {}, function (data) {
            $("#"+serv+"_status").html(data.value)
            busy(false);
        });
    }

    function update_control() {
        busy(true);
        $.get("/service/control/status", {}, function (data) {
            $("#control_status").html(data.value)
            busy(false);
        });
    }

    function upgrade() {
        if (!confirm("在线升级前建议您完整备份数据库，确认现在升级吗？")) {
           return false;
        }
        release = $("#release").val();
        busy(true);
        $.post("/service/upgrade", {release:release}, function (data) {
            $("#inbox").html(data.value)
            busy(false);
            update_status("radiusd");
            update_status("admin");
            update_status("customer");
            update_status("control");
        });
    }

    function initdb() {
        if (!confirm("警告：该操作将会清空数据库，并重建表结构，建议您完整备份数据库，您确认操作吗？")) {
            return false;
        }
        busy(true);
        $.get("/service/initdb", {}, function (data) {
            $("#inbox").html(data.value)
            busy(false);
        });
    }

    function restart_radiusd() {
        busy(true);
        $.get("/service/radiusd/restart", {}, function (data) {
            $("#inbox").html(data.value)
            busy(false);
            update_status("radiusd")
        });
    }

    function restart_admin() {
        busy(true);
        $.get("/service/admin/restart", {}, function (data) {
            $("#inbox").html(data.value)
            busy(false);
            update_status("admin")
        });
    }

    function restart_customer() {
        busy(true);
        $.get("/service/customer/restart", {}, function (data) {
            $("#inbox").html(data.value)
            busy(false);
            update_status("customer")
        });
    }

    function restart_control() {
        busy(true);
        $.ajax({
            url: '/service/control/restart',
            timeout: 3000,
            error: function (xhr, textStatus) {
                busy(false);
                update_status("control")
            },
        });
        setTimeout("update_control()",7000);
    }

    $(document).ready(function () {
        $("#loading").hide();
        $("#inbox").hide();
        $("#upgrade").click(function(){
            upgrade();
        });
        $("#initdb").click(function () {
            initdb();
        });
        $("#restart_radiusd").click(function () {
            restart_radiusd();
        });
        $("#restart_admin").click(function () {
            restart_admin();
        });
        $("#restart_customer").click(function () {
            restart_customer();
        });
        $("#restart_control").click(function () {
            restart_control();
        });

        $("#refresh_radiusd").click(function () {
            update_status("radiusd")
        });
        $("#refresh_admin").click(function () {
            update_status("admin")
        });
        $("#refresh_customer").click(function () {
            update_status("customer")
        });
        $("#refresh_control").click(function () {
            update_status("control")
        });

        update_status("radiusd");
        update_status("admin");
        update_status("customer");
        update_status("control");


    });

</script>
</%def>


<%def name="body()">
<section class="content">
    <div class="box">
    <div class="panel-heading"><i class="fa fa-dashboard"></i> 控制面板
    </div>
    <div class="panel-body">

        <form class="form-inline" role="form">
            <div class="form-group">
                <select id="release" class="form-control">
                    <option value="stable">最新稳定版</option>
                    <option value="master">最新开发版</option>
                </select>
            </div>
            <button type="button" id="upgrade" class="btn btn-success">升级/切换版本</button>
            <button type="button" id="initdb" class="btn btn-success">初始化数据库</button>
        </form>
        <hr/>
        <table class="table table-hover">
            <thead>
            <tr>
                <th width="15%">服务名</th>
                <th>服务状态</th>
                <th width="20%"></th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>Radius认证计费引擎</td>
                <td><span id="radiusd_status"></span></td>
                <td>
                    <button type="button" id="restart_radiusd" class="btn btn-sm btn-info">重启服务</button>
                    <button type="button" id="refresh_radiusd" class="btn btn-sm btn-info">刷新状态</button>
                </td>
            </tr>
            <tr>
                <td>营业管理服务</td>
                <td><span id="admin_status"></span></td>
                <td>
                    <button type="button" id="restart_admin" class="btn btn-sm btn-info">重启服务</button>
                    <button type="button" id="refresh_admin" class="btn btn-sm btn-info">刷新状态</button>
                </td>
            </tr>
            <tr>
                <td>用户自助服务</td>
                <td><span id="customer_status"></span></td>
                <td>
                    <button type="button" id="restart_customer" class="btn btn-sm btn-info">重启服务</button>
                    <button type="button" id="refresh_customer" class="btn btn-sm btn-info">刷新状态</button>
                </td>
            </tr>
            <tr>
                <td>系统控制台</td>
                <td><span id="control_status"></span></td>
                <td>
                    <button type="button" id="restart_control" class="btn btn-sm btn-info">重启服务</button>
                    <button type="button" id="refresh_control" class="btn btn-sm btn-info">刷新状态</button>
                </td>
            </tr>
            </tbody>
        </table>

        <hr>
        <div id="loading" style="display: none;"><img style="max-height: 90px;width: auto;" src="/static/img/loading.gif"></div>
        <div id="inbox" class="text-info" style="height:120px;overflow:auto">

        </div>
    </div>
</div>
    </section>
</%def>

